﻿/// Write a method that returns the index of the first element in array that is bigger than its neighbors, or -1, 
/// if there’s no such element. Use the method from the previous exercise.

using System;

namespace c_sh_II_lek_3
{
    class Zad6
    {
        static void Main()
        {
            int[] arr = { 7, 1, 2, 2, 2, -7, 2, 2 };
            Console.WriteLine(GetFirstBiggerThenNeighbors(arr));
        }


        static int GetFirstBiggerThenNeighbors(int[] array)
        {
            int index = -1;
            for (int i = 0; i < array.Length; i++)
            {
                if (IsBiggerThenNeighbors(array, array[i]))
                {
                    index = i;
                    break;
                }
            }

            return index;
        }

        public static bool IsBiggerThenNeighbors(int[] array, int number)
        {
            bool isBiggerThenNeighbors = false;

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == number)
                {
                    if (CanGo(array.Length, i) && array[i + 1] < number && array[i - 1] < number)
                    {
                        isBiggerThenNeighbors = true;
                    }
                    else
                    {
                        isBiggerThenNeighbors = false;
                    }

                    break;
                }
            }
            return isBiggerThenNeighbors;
        }

        static bool CanGo(int arrayLength, int currentIndex)
        {
            if (currentIndex < 1 || currentIndex > arrayLength - 2)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
